package org.sample.springbootcommandlinerunner;

import org.sample.springbootcommandlinerunner.web.WebConnRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import reactor.netty.resources.ConnectionProvider;

@SpringBootApplication
public class Application implements CommandLineRunner {

	private static Logger LOG = LoggerFactory.getLogger(Application.class);

	@Autowired
	private WebConnRunner webConnRunner;

	public static void main(String[] args) {
		LOG.info("STARTING THE APPLICATION");
		SpringApplication.run(Application.class, args);
		LOG.info("APPLICATION FINISHED");
	}

	@Bean
	public WebClient webClient() {

		var connProvider = ConnectionProvider.builder("webclient-conn-pool")
				.maxConnections(10000).build();
		var httpClient = HttpClient.create(connProvider);
		var connector = new ReactorClientHttpConnector(httpClient);

		return WebClient.builder()
				.clientConnector(connector)
				.build();
	}

	public void run(String... args) {

		LOG.info("Executing command line.");

		webConnRunner.run();
	}

}
